<?php
class PagesController extends AppController{
	
	public $helpers = array('Html', 'Form');
	public $components = array('Session');
	
	public function beforeFilter() {
		$this->Auth->deny('index');
	}
	
	public $paginate = array(
        'limit' => 10,
		'order' => array(
            'Page.id' => 'desc'
        )
    );
	
	function index(){
		
		$pages=$this->paginate('Page');
		
		$this->set('title_for_layout', 'Page');
		$this->set('Pages', $pages);
		
	}
	
	
	function add($id=0){
		
		$this->set('title_for_layout', 'Create Page');
		$base_url = 'http://'.$_SERVER['HTTP_HOST'].$this->base ;
		$this->set("base_url",$base_url);
		$this->set("menu_id",$id);
		
		$this->loadModel('Menu');
		$menus_data=$this->Page->query("select * from menus");
		
		$this->set('menus',$menus_data);
		
		//find footer menu
		$footer_menus=$this->Page->query("select * from footer_menus");
		
		$this->set('footer_menus',$footer_menus);
		
		if($this->request->is('post')){
			$this->request->data['Page']['user_id'] = $this->Auth->user('id'); //Added this line
			
			if($this->Page->save($this->request->data)){
				$this->Session->setFlash('Your post has been saved.');
				$this->redirect(array('action' => 'index'));
			}
		
		}
	}
	
	function edit($id = null){
		$this->set('title_for_layout', 'Edit Page');
		$base_url = 'http://'.$_SERVER['HTTP_HOST'].$this->base ;
		$this->set("base_url",$base_url);
	
		$this->Page->id = $id;
		
		$menus_data=$this->Page->query("select * from menus");
		
		$this->set('menus',$menus_data);
		
		if ($this->request->is('get')) {

			$this->request->data = $this->Page->read();
			//$this->request->data = $this->Page->query("select * from pages where id='".$id."'");;
			
			//$read = $this->Page->read();
			//print_r($read);
		} else {
			if ($this->Page->save($this->request->data)) {
				$this->Session->setFlash('Your post has been updated.');
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash('Unable to update your post.');
			}
		}
	
	}
	
	
	function delete($id){
		
		if ($this->request->is('get')) {
			throw new MethodNotAllowedException();
		}
		
		if ($this->Page->delete($id)) {
			$this->Session->setFlash('The Page with id: ' . $id . ' has been deleted.');
			$this->redirect(array('action' => 'index'));
		}
		
	}
	
	public function isAuthorized($user) {
		// All registered users can add posts
		if ($this->action === 'add') {
			return true;
		}
		
		if ($this->action === 'index') {
			
			if (isset($user['role']) && $user['role'] === 'admin') {
				return true;
			}else{
				$this->Session->setFlash('You are not authorised user');
				$this->redirect('/users/login/');
			}
		}
		
		
		// The owner of a post can edit and delete it
		if (in_array($this->action, array('edit', 'delete'))) {
			$PageId = $this->request->params['pass'][0];
			if ($this->Page->isOwnedBy($PageId, $user['id'])) {
				return true;
			}else{
			
				return false;
			}
		}

		return parent::isAuthorized($user);
	}
	
}
?>